/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 *  \file     Port_Types.h                                                                              *
 *  \brief    his file contains data structures header for PORT MCAL driver.                            *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2023/07/14     <td>1.0.0                                                                     *
 * </table>                                                                                             *
 *******************************************************************************************************/

#ifndef PORT_TYPES_H
#define PORT_TYPES_H
#ifdef __cplusplus
extern "C" {
#endif

/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Std_Types.h"
/**
 *  \addtogroup MCAL_PORT_MACRO PORT Macro.
 *
 * This files defines PORT macros.
 *  @{
 */

/**
 *  \name Port Driver Error Codes.
 *
 *  The Error Codes is one of the argument to Det_ReportError function and is
 *  used to identify the type of the error.
 *  @{
 */
/** \brief Invalid Port Pin ID requested */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_PARAM_PIN                ((Std_ReturnType)0x0A)
/** \brief Port Pin not configured as changeable */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_DIRECTION_UNCHANGEABLE   ((Std_ReturnType)0x0B)
/** \brief API Port_Init service called with wrong parameter */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_INIT_FAILED              ((Std_ReturnType)0x0C)
/** \brief Port Pin Mode passed not valid */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_PARAM_INVALID_MODE       ((Std_ReturnType)0x0D)
/** \brief API Port_SetPinMode service called when mode is unchangeable */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_MODE_UNCHANGEABLE        ((Std_ReturnType)0x0E)
/** \brief API service called without module initialization */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_UNINIT                   ((Std_ReturnType)0x0F)
/** \brief APIs called with a Null Pointer */
/** Traceability       : SWSR_PORT_016 */
#define PORT_E_PARAM_POINTER            ((Std_ReturnType)0x10)

/** \brief  Error due to API duplicated invoked Port_Init() */
#define PORT_E_INIT                      ((Std_ReturnType)0x80U)
/** \brief  Error due to invalid port config option type */
#define PORT_E_CONFIG_TYPE               ((Std_ReturnType)0x81U)
/** \brief  Error due to invalid port config parameters */
#define PORT_E_CONFIG_PARAM              ((Std_ReturnType)0x82U)
/** @} */

/**
 *  \name Port Driver PIN MUX.
 *
 *  The PIN MUX is one of the argument to Port_SetPinMode function and is
 *  used to identify the type of the mux.
 *  @{
 */
/** port pin mux index 0 */
#define  PORT_PIN_MUX_ALT0  0U
/** port pin mux index 1 */
#define  PORT_PIN_MUX_ALT1  1U
/** port pin mux index 2 */
#define  PORT_PIN_MUX_ALT2  2U
/** port pin mux index 3 */
#define  PORT_PIN_MUX_ALT3  3U
/** port pin mux index 4 */
#define  PORT_PIN_MUX_ALT4  4U
/** port pin mux index 5 */
#define  PORT_PIN_MUX_ALT5  5U
/** port pin mux index 6 */
#define  PORT_PIN_MUX_ALT6  6U
/** port pin mux index 7 */
#define  PORT_PIN_MUX_ALT7  7U
/** port pin mux index 8 */
#define  PORT_PIN_MUX_ALT8  8U
/** port pin mux index 9 */
#define  PORT_PIN_MUX_ALT9  9U
/** @} */
/********************************************************************************************************
 *                                  Global Types definition                                             *
 *******************************************************************************************************/
/**
 *  \addtogroup MCAL_PORT_CFG PORT Configuration
 *  @{
 *
 */

/** \brief open drain mode of a port pin */
typedef enum
{
    /** port pin push pull mode */
    PORT_PIN_PUSH_PULL   = 0U,
    /** port pin open drain mode */
    PORT_PIN_OPEN_DRAIN  = 1U,
} Port_OpenDrainType;

/** \brief force input mode of a port pin*/
typedef enum
{
    /** port pin force input disable */
    PORT_PIN_FORCE_INPUT_NORMAL  = 0U,
    /** port pin force input enable, read back value is output value */
    PORT_PIN_FORCE_INPUT_ENABLE  = 1U,
    /** port pin force input enable, read back value is 1 */
    PORT_PIN_FORCE_INPUT_HIGH    = 2U,
    /** port pin force input enable, read back value is 0 */
    PORT_PIN_FORCE_INPUT_LOW     = 3U,
    /** port pin force input enable, Can read back output values using GPIO */
    PORT_PIN_FORCE_INPUT_GPIO_READBACK = 4U,
} Port_ForceInputType;

/** \brief force output mode of a port pin*/
typedef enum
{
    /** port pin force output disable */
    PORT_PIN_FORCE_OUTPUT_DISABLE  = 0U,
    /** port pin force output enable, PAD fault status is 1 */
    PORT_PIN_FORCE_OUTPUT_HIGH    = 1U,
    /** port pin force output enable, PAD fault status is 0 */
    PORT_PIN_FORCE_OUTPUT_LOW     = 2U,
} Port_ForceOutputType;

/** \brief pulls mode of a port pin */
typedef enum
{
    /** port pin no pull */
    PORT_PIN_NOPULL     = 0U,
    /** port pin pull down */
    PORT_PIN_PULL_DOWN  = 1U,
    /** port pin pull up */
    PORT_PIN_PULL_UP    = 3U,
} Port_PullConfigType;

/** \brief drive strength of a port pin */
typedef enum
{
    /** port pin drive strength 2ma */
    PORT_PIN_DS_2MA  = 0U,
    /** port pin drive strength 8ma */
    PORT_PIN_DS_8MA  = 1U,
    /** port pin drive strength 4ma */
    PORT_PIN_DS_4MA  = 2U,
    /** port pin drive strength 12ma */
    PORT_PIN_DS_12MA = 3U,
} Port_DriveStrengthType;

/** \brief slew rate of a port pin */
typedef enum
{
    /** port pin slew rate fast */
    PORT_PIN_SR_FAST  = 0U,
    /** port pin slew rate slow */
    PORT_PIN_SR_SLOW  = 1U,
} Port_SlewRateType;

/** \brief input select CMOS mode of a port pin */
typedef enum
{
    /** input select CMOS mode */
    PORT_PIN_IS_CMOS          = 0U,
    /** input select CMOS SCHMITT mode */
    PORT_PIN_IS_CMOS_SCHMITT  = 1U,
} Port_ISCmosSchmittType;

/** \brief pad mode select of a port pin, analog-digital combo IO only */
typedef enum
{
    /** analog pin mode select 3V3 */
    PORT_PIN_SETTING_ANALOG_3V3  = 0U,
    /** analog pin mode select 5V */
    PORT_PIN_SETTING_ANALOG_5V = 1U,
} Port_AnalogInputType;

/** \brief initial level value of a port pin, GPIO only */
typedef enum
{
    /** gpio pin level low */
    PORT_PIN_LEVEL_LOW  = 0U,
    /** gpio pin level high */
    PORT_PIN_LEVEL_HIGH = 1U,
} Port_LevelType;


/** \brief direction of a port pin, GPIO only*/
/** Traceability       : SWSR_PORT_026 SWSR_PORT_027 SWSR_PORT_028 */
typedef enum
{
    /** gpio pin input direction */
    PORT_PIN_IN  = 0U,
    /** gpio pin output direction */
    PORT_PIN_OUT = 1U,
} Port_PinDirectionType;

/** \brief interrupt mode of a port pin, GPIO only */
typedef enum
{
    /** interrupt disable*/
    PORT_PIN_INTERRUPT_DISABLED          = 0U,
    /** sync interrupt low level*/
    PORT_PIN_INTERRUPT_LOW_LEVEL         = 1U,
    /** sync interrupt high level*/
    PORT_PIN_INTERRUPT_HIGH_LEVEL        = 2U,
    /** sync interrupt rising edge*/
    PORT_PIN_INTERRUPT_RISING_EDGE       = 3U,
    /** sync interrupt falling edge*/
    PORT_PIN_INTERRUPT_FALLING_EDGE      = 4U,
    /** sync interrupt both edge*/
    PORT_PIN_INTERRUPT_BOTH_EDGE         = 5U,
    /** async interrupt low level*/
    PORT_PIN_ASYC_INTERRUPT_LOW_LEVEL    = 6U,
    /** async interrupt high level*/
    PORT_PIN_ASYC_INTERRUPT_HIGH_LEVEL   = 7U,
    /** async interrupt rising edge*/
    PORT_PIN_ASYC_INTERRUPT_RISING_EDGE  = 8U,
    /** async interrupt falling edge*/
    PORT_PIN_ASYC_INTERRUPT_FALLING_EDGE = 9U,
    /** async interrupt both edge*/
    PORT_PIN_ASYC_INTERRUPT_BOTH_EDGE    = 10U,
} Port_InterruptConfigType;

/** \brief Symbolic name of a port pin. */
/** Traceability       : SWSR_PORT_023 SWSR_PORT_024 SWSR_PORT_025 */
typedef uint16 Port_PinType;

/** \brief pad function pin mux of a port pin */
/** Traceability       : SWSR_PORT_029 SWSR_PORT_030 SWSR_PORT_031 SWSR_PORT_032 */
typedef uint32 Port_PinModeType;

/** \brief Common PINCTRL configuration parameters define*/
typedef struct
{
    /** port pin index */
    uint32                          pinIndex;
    /** port pin mux modeChangable*/
    boolean                         modeChangable;
    /** port pin mux*/
    Port_PinModeType                mux;
    /** port pin opendrain or push pull*/
    Port_OpenDrainType              openDrain;
    /** port pin pulls mode*/
    Port_PullConfigType             pullConfig;
    /** port pin drive strength*/
    Port_DriveStrengthType          driveStrength;
    /** port pin slew rate*/
    Port_SlewRateType               slewRate;
    /** port pin input select cmos schmitt type*/
    Port_ISCmosSchmittType          inputSelect;
    /** port pin direction, for gpio only*/
    Port_PinDirectionType           dataDirection;
    /** port pin interrupt config, for gpio only*/
    Port_InterruptConfigType        interruptConfig;
    /** port pin initial level value, for gpio only*/
    Port_LevelType                  initialValue;
    /** port pin force input*/
    Port_ForceInputType             forceInput;
    /** port pin force output*/
    Port_ForceOutputType            forceOutput;
    /** port pin direction changable, for gpio only*/
    boolean                         directChangable;
} Port_SettingsConfigType;

/** \brief analog configuration parameters define */
typedef struct
{
    /** port pin index */
    uint32                          pinIndex;
    /** port pin mode select, for analog pin only */
    Port_AnalogInputType            inputLevel;
} Port_AnalogConfigType;

/** \brief xtrgoi output configuration parameters define */
typedef struct
{
    /** port pin index */
    uint32                          pinIndex;
    /** port pin mux*/
    Port_PinModeType                mux;
} Port_XtrgOutputConfigType;


/** \brief  Type of the external data structure containing the initialization data for port module. */
/** Traceability       : SWSR_PORT_020 SWSR_PORT_021 SWSR_PORT_022 */
typedef struct
{
    /** Number of pads (to be configured) */
    VAR(uint16, PORT_VAR) num;
    /** Pad common configuration */
    P2CONST(Port_SettingsConfigType, AUTOMATIC, PORT_APPL_CONST) padCfgPtr;
    /** Number of analog pads (to be configured) */
    VAR(uint16, PORT_VAR) analogNum;
    /** Analog pad configuration */
    P2CONST(Port_AnalogConfigType, AUTOMATIC, PORT_APPL_CONST) analogCfgPtr;
} Port_ConfigType;
/** @} */

/** \brief  input source select of a function */
typedef struct
{
    uint32  pin;
    uint32  mux;
    uint32  base;
    uint32  offset;
    uint32  value;
} Port_InputSelectType;

/* Port device MAP type */
typedef struct
{
    uint32 pinStart;            /* pin start index */
    uint32 pinNum;              /* pin number */
    uint32 ctrlIdx;             /* pin ctrl Idx*/
    uint32 ctrlBase;            /* pin ctrl base */
} Port_PinMapType;

#ifdef __cplusplus
}
#endif
/* End of file */
#endif /* PORT_TYPES_H */
